#include "DataExchange.h"

void DataExchange::ExportStep(const QString& fileName, STEPControl_StepModelType stepType)
{
    IFSelect_ReturnStatus aStatus;

    STEPControl_Writer aStepWriter;
    for (auto aisObj : aisObjs)
    {
        if (Handle(MAisDemo) aShape = Handle(MAisDemo)::DownCast(aisObj))
        {
            aStatus = aStepWriter.Transfer(aShape->Shape() , stepType);
            if (aStatus != IFSelect_RetDone)
            {
                std::cout << "A shape was not transferred successfully" << std::endl;
                return;
            }
        }
    }
    aStatus = aStepWriter.Write(fileName.toStdString().c_str());

    switch (aStatus)
    {
    case IFSelect_RetError:
        std::cout << "Incorrect Data." << std::endl;
        break;
    case IFSelect_RetFail:
        std::cout << "Writing error" << std::endl;
        break;
    case IFSelect_RetVoid:
        std::cout << "Nothing to transfer." << std::endl;
        break;
    case IFSelect_RetStop:
    case IFSelect_RetDone:
        std::cout << "A STEP file was successfully written" << std::endl;
        break;
    }
}

void DataExchange::ImportStep(const QString& fileName)
{
    STEPControl_Reader aReader;
    const IFSelect_ReturnStatus aStatus = aReader.ReadFile(fileName.toStdString().c_str());
    if (aStatus != IFSelect_RetDone)
    {
        std::cout << "A BREP file was not read successfully" << std::endl;
        return;
    }

    bool anIsFailsOnly = false;
    aReader.PrintCheckLoad(anIsFailsOnly , IFSelect_ItemsByEntity);

    int aRootsNumber = aReader.NbRootsForTransfer();
    aReader.PrintCheckTransfer(anIsFailsOnly , IFSelect_ItemsByEntity);
    for (int i = 1; i <= aRootsNumber; i++)
    {
        aReader.TransferRoot(i);
    }

    aisObjs.clear();
    int aShapesNumber = aReader.NbShapes();
    for (int i = 1; i <= aShapesNumber; i++)
    {
        TopoDS_Shape aTopoShape = aReader.Shape(i);
        Handle(MAisDemo) anAisShape = new MAisDemo(aTopoShape);
        aisObjs.push_back(anAisShape);
    }
    std::cout << "A STEP file was read successfully" << std::endl;
}

void DataExchange::ExportIges(const QString& fileName)
{
    IGESControl_Controller::Init();
    IGESControl_Writer anIgesWriter(Interface_Static::CVal("XSTEP.iges.unit") ,
                                    Interface_Static::IVal("XSTEP.iges.writebrep.mode"));

    bool anIsShapeExist = false;
    for (auto aisObj : aisObjs)
    {
        if (Handle(MAisDemo) aShape = Handle(MAisDemo)::DownCast(aisObj))
        {
            anIsShapeExist = Standard_True;
            anIgesWriter.AddShape(aShape->Shape());
        }
    }

    if (anIsShapeExist)
    {
        anIgesWriter.ComputeModel();
        if (anIgesWriter.Write(fileName.toStdString().c_str()))
        {
            std::cout << "A IGES file was successfully written" << std::endl;
        }
        else
        {
            std::cout << "A IGES file was not written" << std::endl;
        }
    }
    else
    {
        std::cout << "Shapes do not exist" << std::endl;
    }
}

void DataExchange::ImportIges(const QString& fileName)
{
    IGESControl_Reader aReader;
    int status = aReader.ReadFile(fileName.toStdString().c_str());
    if (status != IFSelect_RetDone)
    {
        std::cout << "A IGES file was not read successfully" << std::endl;
        return;
    }
    aReader.TransferRoots();
    TopoDS_Shape aTopoShape = aReader.OneShape();
    Handle(MAisDemo) anAisShape = new MAisDemo(aTopoShape);

    aisObjs.clear();
    aisObjs.push_back(anAisShape);
    std::cout << "A IGES file was read successfully" << std::endl;
}

void DataExchange::ImportObj(const QString& fileName)
{
    RWObj_TriangulationReader reader;
    Message_ProgressRange process;
    bool status = reader.Read(fileName.toStdString().c_str() , process);

    TopoDS_Shape shape = reader.ResultShape();

    Handle(MAisDemo) anAisShape = new MAisDemo(shape);

    aisObjs.clear();
    aisObjs.push_back(anAisShape);
    std::cout << "A OBJ file was read successfully" << std::endl;
}
